package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.commom.result.Result;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangYK
 * @version 8.0
 * @title AdminController
 * @description
 * @create 2024/7/5 20:49
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/acl/user")
@CrossOrigin
public class AdminController {
    @Resource
    private AdminService adminService;

    @Resource
    private RoleService roleService;

    //7、
    //获取某个用户的所有角色
    // url: `${api_name}/toAssign/${adminId}`,
    //    method: 'get'
    @ApiOperation("获取某个用户的所有角色")
    @GetMapping("toAssign/{adminId}")
    public Result toAssign(@PathVariable Integer adminId) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(adminId);
        return Result.ok(roleMap);
    }


    //8、给某个用户分配角色
    //roleId的结构: 字符串, 'rId1,rId2,rId3'
    //url: `${api_name}/doAssign`,
    //    method: 'post',
    //    params: {
    //      adminId,
    //      roleId
    //    }
    @ApiOperation("给某个用户分配角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long adminId,
                           @RequestParam Long[] roleId) {
        roleService.saveUserRoleRelationShip(adminId,roleId);
        return Result.ok(null);
    }

    //1、用户列表
    //  url: `${api_name}/${page}/${limit}`,
    //    method: 'get',
    //    params: searchObj
    @ApiOperation(value = "用户列表")
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit,
                       AdminQueryVo adminQueryVo) {
        Page<Admin> adminPage = new Page<>(page,limit);
        IPage<Admin> pageModel = adminService.selectPageUser(adminPage,adminQueryVo);
        return Result.ok(pageModel);
    }

    //2、id查询用户
    // url: `${api_name}/get/${id}`,
    //    method: 'get'
    @ApiOperation(value = "根据id查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    //3、添加用户
    // url: `${api_name}/save`,
    //    method: 'post',
    //    data: user
    @ApiOperation(value = "添加用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin) {
        boolean flag = adminService.save(admin);
        if (flag){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    //4、修改用户
    // url: `${api_name}/update`,
    //    method: 'put',
    //    data: user
    @ApiOperation(value = "修改用户")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin) {
        boolean flag = adminService.updateById(admin);
        if (flag){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    //5、id删除用户
    //url: `${api_name}/remove/${id}`,
    //    method: 'delete'
    @ApiOperation(value = "id删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean flag = adminService.removeById(id);
        if (flag){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    //6、批量删除用户
    // url: `${api_name}/batchRemove`,
    //    method: 'delete',
    //    data: ids
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        boolean flag = adminService.removeByIds(ids);
        if (flag){
            return Result.ok(null);
        }
        return Result.fail(null);
    }
}
